package com.example.opengldemo._05_;

import android.opengl.GLES30;

import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.ShaderUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class Cube {

    String vertexRes  = "four/vertex.sh";
    String fragmentRes  = "four/fragment.sh";

    int mProgram ;

    int m_A_PositionHandle ;
    int m_A_ColorHandle ;
    int m_U_MVPMatrixHandle ;

    FloatBuffer vertaxBuffer ;
    FloatBuffer fragBuffer ;

    int vCount ;
    float mShape ;
    float myShape ;

    Cube(MySurfaceView view){
        initBufferData();
        initHandle(view) ;
    }

    private void initHandle(MySurfaceView view) {
        mProgram = ShaderUtil.createProgram(
                ShaderUtil.loadFromAssetsFile(vertexRes , view.getResources()) ,
                ShaderUtil.loadFromAssetsFile(fragmentRes , view.getResources())
        );

        m_A_PositionHandle = GLES30.glGetAttribLocation(mProgram , "aPosition");
        m_A_ColorHandle = GLES30.glGetAttribLocation(mProgram , "aColor");
        m_U_MVPMatrixHandle = GLES30.glGetUniformLocation(mProgram , "uMVPMatrix");
    }

    private void initBufferData() {
        float[] vertexArray = getCubeData();
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertexArray.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertaxBuffer = byteBuffer.asFloatBuffer();
        vertaxBuffer.put(vertexArray);
        vertaxBuffer.position(0);

        float[] colorArray = getFragData();
        byteBuffer = ByteBuffer.allocateDirect(colorArray.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        fragBuffer = byteBuffer.asFloatBuffer();
        fragBuffer.put(colorArray);
        fragBuffer.position(0);

    }
    private float[] getFragData() {
        float[] colorArray = new float[vCount * 4];//顶点着色数据的初始化
        for (int i = 0; i < vCount; i++) {
            if (i % 3 == 0) { //中心点为白色，RGBA 4 个通道[1,1,1,0]
                colorArray[i * 4] = 1;
                colorArray[i * 4 + 1] = 1;
                colorArray[i * 4 + 2] = 1;
                colorArray[i * 4 + 3] = 0;
            } else { //边上的点为淡蓝色，RGBA 4 个通道[0.45,0.75,0.75,0]
                colorArray[i * 4] = 0.45f;
                colorArray[i * 4 + 1] = 0.75f;
                colorArray[i * 4 + 2] = 0.75f;
                colorArray[i * 4 + 3] = 0;
            }
        }
        return colorArray;
    }

    public float[] getFragColorData(){
        return new float[]{
                //«∞√Ê
                1,1,1,0,
                1,0,0,0,
                1,0,0,0,
                1,1,1,0,
                1,0,0,0,
                1,0,0,0,
                1,1,1,0,
                1,0,0,0,
                1,0,0,0,
                1,1,1,0,
                1,0,0,0,
                1,0,0,0,
                1,1,1,0,
                0,0,1,0,
                0,0,1,0,
                1,1,1,0,
                0,0,1,0,
                0,0,1,0,
                1,1,1,0,
                0,0,1,0,
                0,0,1,0,
                1,1,1,0,
                0,0,1,0,
                0,0,1,0,
                1,1,1,0,
                1,0,1,0,
                1,0,1,0,
                1,1,1,0,
                1,0,1,0,
                1,0,1,0,
                1,1,1,0,
                1,0,1,0,
                1,0,1,0,
                1,1,1,0,
                1,0,1,0,
                1,0,1,0,
                1,1,1,0,
                1,1,0,0,
                1,1,0,0,
                1,1,1,0,
                1,1,0,0,
                1,1,0,0,
                1,1,1,0,
                1,1,0,0,
                1,1,0,0,
                1,1,1,0,
                1,1,0,0,
                1,1,0,0,
                1,1,1,0,
                0,1,0,0,
                0,1,0,0,
                1,1,1,0,
                0,1,0,0,
                0,1,0,0,
                1,1,1,0,
                0,1,0,0,
                0,1,0,0,
                1,1,1,0,
                0,1,0,0,
                0,1,0,0,
                1,1,1,0,
                0,1,1,0,
                0,1,1,0,
                1,1,1,0,
                0,1,1,0,
                0,1,1,0,
                1,1,1,0,
                0,1,1,0,
                0,1,1,0,
                1,1,1,0,
                0,1,1,0,
                0,1,1,0,
        };
    }

    public void drawSelf(){
        GLES30.glUseProgram(mProgram);

        GLES30.glUniformMatrix4fv(m_U_MVPMatrixHandle , 1 , false , MatrixState.getFinalMatrix() , 0);
        GLES30.glVertexAttribPointer(m_A_PositionHandle , 3 , GLES30.GL_FLOAT , false , 3*4 , vertaxBuffer);
        GLES30.glVertexAttribPointer(m_A_ColorHandle , 4 , GLES30.GL_FLOAT , false , 4*4 , fragBuffer);

        GLES30.glEnableVertexAttribArray(m_A_ColorHandle);
        GLES30.glEnableVertexAttribArray(m_A_PositionHandle);

        GLES30.glDrawArrays(GLES30.GL_TRIANGLES , 0  ,vCount );

        GLES30.glDisableVertexAttribArray(m_A_PositionHandle);
        GLES30.glDisableVertexAttribArray(m_A_ColorHandle);

    }
    
    public float[] getCubeData(){
        vCount=12*6;
        final float UNIT_SIZE = 0.2f ;
        return new float[]
                {
                        //«∞√Ê
                        0,0,UNIT_SIZE,
                        UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
                        -UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
                        0,0,UNIT_SIZE,
                        -UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
                        -UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        0,0,UNIT_SIZE,
                        -UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        0,0,UNIT_SIZE,
                        UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,

                        0,0,-UNIT_SIZE,
                        UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        0,0,-UNIT_SIZE,
                        UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        -UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        0,0,-UNIT_SIZE,
                        -UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        -UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        0,0,-UNIT_SIZE,
                        -UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,

                        -UNIT_SIZE,0,0,
                        -UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
                        -UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        -UNIT_SIZE,0,0,
                        -UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        -UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        -UNIT_SIZE,0,0,
                        -UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        -UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        -UNIT_SIZE,0,0,
                        -UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        -UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
                        //”“√Ê
                        UNIT_SIZE,0,0,
                        UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
                        UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        UNIT_SIZE,0,0,
                        UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        UNIT_SIZE,0,0,
                        UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        UNIT_SIZE,0,0,
                        UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,

                        0,UNIT_SIZE,0,
                        UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
                        UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        0,UNIT_SIZE,0,
                        UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        -UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        0,UNIT_SIZE,0,
                        -UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
                        -UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
                        0,UNIT_SIZE,0,
                        -UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
                        UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,

                        0,-UNIT_SIZE,0,
                        UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        -UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        0,-UNIT_SIZE,0,
                        -UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                        -UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        0,-UNIT_SIZE,0,
                        -UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        0,-UNIT_SIZE,0,
                        UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
                        UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
                };
 
    }
}
